package cn.itcast.web.controller.company;

import cn.itcast.domain.company.Company;

import cn.itcast.service.company.CompanyService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@Controller
@RequestMapping("/company")
@Slf4j
public class CompanyController {
    @Reference
    private CompanyService companyService;


    /**
    * 企业列表
    */
    /*@RequestMapping("/list")
    public ModelAndView list(){
        log.info("执行查询企业列表");
        List<Company> list = companyService.findAll();

        ModelAndView mv = new ModelAndView("company/company-list");
        mv.addObject("list",list);

        return mv;
    }*/

    /*企业列表-分页查询*/
    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5")Integer pageSize){
        log.info("执行查询企业列表");
        PageInfo<Company> pageInfo = companyService.findByPage(pageNum, pageSize);

        ModelAndView mv = new ModelAndView("company/company-list");
        mv.addObject("pageInfo",pageInfo);

        return mv;
    }


    /**
    *企业：进入添加页面
    *功能入口：company-list.jsp点击新建
    *请求地址：http://locahost:8080/company/toAdd.do
    *响应地址：http://locahost:8080/WEB-INF/pages/company/company-add.jsp
    */
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "company/company-add";
    }


    /**
    *企业：根据请求参数是否带id执行
     * 添加/更新操作
    *功能入口：company-add.jsp点击提交
    *请求地址：http://locahost:8080/company/edit.do
    *请求数据：封装成Company对象
    *响应地址：添加成功，重定向到企业列表/company/list，重新查询显示添加的数据
     * /company/toUpdate.do?id=${item.id}
    */

    @RequestMapping("/edit")
    public String edit(Company company){
        if(StringUtils.isEmpty(company.getId())){
            companyService.save(company);
        }
        else{
            companyService.update(company);
        }
        return "redirect:/company/list";
    }


    /**
    *企业：进入更改信息页面
    *功能入口：company-list.jsp点击编辑
    *请求地址：http://locahost:8080/company/toUpdate.do?id=n
    *响应地址：http://locahost:8080/WEB-INF/pages/company/company-update.jsp
    */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id, Model model){
        Company company = companyService.findById(id);
        model.addAttribute("company",company);
        return "company/company-update";
    }


    /**
    *企业：删除一条企业信息
    *功能入口：company-list.jsp点击删除
    *请求地址：http://locahost:8080/company/delete.do?id=5
    *响应地址：删除成功重定向到/company/list";
    */
    @RequestMapping("/delete")
    public String delete(String id){
        companyService.delete(id);
        return "redirect:/company/list";
    }








    /*
    * 测试类型转换，异常
    * */
    @RequestMapping("test")
    public String test(@RequestParam("birth")Date birthday){
        String str =null;
        str.length();
        System.out.println(birthday);
        return "success";
    }






}
